# -*- coding: UTF-8 -*-
import pandas as pd
import time,os,datetime,math,struct,sys
from colorama import init, Fore, Back, Style
# from makedir import FileOp
pd.set_option('display.expand_frame_repr', False)
init(autoreset=True)
import logging
from loguru import logger
from struct import *
# from tqdm import tqdm
currentDate = time.strftime("%Y%m%d", time.localtime())
LOG_DIR = os.path.expanduser("logs/")
LOG_FILE = os.path.join(LOG_DIR, currentDate+"read_big_csv.log")
logger.add(LOG_FILE,rotation = "2000KB" , encoding='gb2312')   #压缩成zip格式

#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
import configparser
class Config:
    def __init__(self,filename,encoding="gbk"):
        logger.info("Config:def __init__(self):初始化"+filename+encoding)
        self.cf = configparser.ConfigParser()
        self.cf.read(filename, encoding=encoding)  # 读取配置文件，如果写文件的绝对路径，就可以不用os模块
    def get_config(self,rootstr,txt):
        return self.cf.get(rootstr, txt)

@logger.catch
def show_title(title,funcxxx, **kwargs):
    logger.info('show_title')

    starttime = datetime.datetime.now()

    funcxxx(title,**kwargs)

    endtime = datetime.datetime.now()
    logger.success('程序运行时间:'+ str(endtime - starttime))

@logger.catch
def set_running(title):
    logger.info('set_running')
    cf = Config("config.ini")
    currentDate = time.strftime("%Y%m%d", time.localtime())
    currentDate = currentDate + "_final"
    targetDir = "E:\\tdx_export\\hdf"
    logger.success(targetDir)
    dir_list=os.listdir(targetDir)
    dirfilecount=len(dir_list)

    scale=dirfilecount
    logger.success(targetDir)
    print(scale)
    LL=40
    color_para=Fore.RED
    i=0

    for __list in dir_list:
        i += 1
        msg = ""  # getattr(funcxxx, '__name__')
        logger.info(targetDir+"\\"+__list)
        # if __list[-3:]=="CSV":
        #     df = pd.read_csv(targetDir + "\\" + __list)
        # if __list[-2:] == "h5":
        df = pd.read_hdf(targetDir+"\\"+__list,'df9')
        print(df)
        df_size=df.shape[0]
        a = (i / scale) * 100
        b = '.' * LL
        c = '▋' * (int((i * LL) / scale))
        b = '.' * ((LL - 1) - int((i * LL) / scale))
        dur = time.perf_counter()  # //每次获取当前时间
        msg1 = ""
        msg2 = ""
        # print(__list)
        print(Style.BRIGHT + Fore.RED + "\r[{}]{:^3.0f}% {}{} {:.2f}s".format(i, a, c, b, dur), df_size, end=' ')
        # time.sleep(0.01)
        if i == 1:
            break
    print("循环", i)
    # print(char)
    for i in range(1,1100):
        df = pd.read_hdf("E:\\tdx_export\\hdf\\bigdata.h5", 'df' + str(i))
        if i==1:
            dftmp = df
        else:
            df =pd.concat([df,dftmp])
            dftmp=df
    # print(df)
    df_size = df.shape[0]
    logger.info(df_size)
if __name__ == '__main__':
    os.system("cls")
    show_title("title",funcxxx=set_running)
